package implementation;

import java.util.ArrayList;
import java.util.List;


import entility.Job;
import entility.JobImpl;
import entility.Machine;
import entility.Notification;
import entility.Range;

public class OutrosHosts {

	@Override
	public String toString() {
		return "OutrosHosts [host=" + host + ", lineNumber=" + lineNumber
				+ ", values=" + values + ", range=" + range + ", maxNumber="
				+ maxNumber + "]";
	}

	public Machine host;
	public int lineNumber;
	public List<Integer> values;
	public Range range;
	public int maxNumber = 5000;

	public OutrosHosts(Machine host, Range range)
	{
		this.range = range;
		this.host = host;
		this.lineNumber = 0;
		this.values = new ArrayList<Integer>();
	}

	public Boolean canInsert(Integer value)
	{
		return range.contains(value);
	}

	public void Insert(Integer value)
	{
		try{
			this.values.add(value);
			if(lineNumber > maxNumber )
			{
				Job newJob = new JobImpl(Notification.HOST);
				newJob.setMethod("addToJobList");
				newJob.setDataInt(new ArrayList<Integer>(values));
				newJob.setHead("redirect");
				try {
					if(Notification.print)
						System.out.println("Send to " + host  + "50000 linhas");
					this.host.getMessage(newJob);

				} catch (Exception e) {
					if(Notification.print)
						System.out.println("socket Fechado tentar mandar denovo");
					Thread.sleep(100);
					values.remove(value);
					Insert(value);
				
				}
				this.values.clear();
				this.lineNumber = 0;

			}else
			{
				this.lineNumber++;

			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}

	}

}
